﻿using System;
using System.Collections.Generic;
using System.Numerics;
namespace Primer
{
    internal partial class TumblerCollection : ForwardBlockedCollection<Tumbler, BigInteger>
    {
        public BigInteger ID = 1;
        private uint _mark;

        protected override Tumbler Factory(ref BigInteger value) { return new Tumbler(ref value, ID % value, _mark); }

        public bool Tumble()
        {
            ID++;
            int i;
            Tumbler[] a;
            if (false && _mark++ < uint.MaxValue)
            {
                foreach (var block in Blocks)
                    for (i = 0, a = block.Items; i < block.Count; i++)
                        if (Tumbler.Tumble(ref a[i], _mark))
                            return true;
                return false;
            }
            _mark = 0;
            var hit = false;
            foreach (var block in Blocks)
                for (i = 0, a = block.Items; i < block.Count; i++)
                    hit |= Tumbler.Tumble(ref a[i], 0);
            return hit;
        }

        public bool TumbleFast()
        {
            ID++;
            return false;
        }

        public int Count
        {
            get
            {
                var blocks = Blocks.Count;
                var count = (blocks > 0 ? (blocks - 1) * BlockSize : 0);
                var last = Blocks.Last;
                return count + (last != null ? last.Value.Count : 0);
            }
        }

        public IEnumerable<BigInteger> Get(long index)
        {
            return null;
        }
    }
}
